package com.gr3g.dev.learndraw.model.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.gr3g.dev.learndraw.LevelsActivity;
import com.gr3g.dev.learndraw.R;
import com.gr3g.dev.learndraw.model.Dessin;

/** @author Gregory Tardivel */
public final class LevelView extends LinearLayout {

    /** Largeur de l'ecran. */
    private float width;
    /** Hauteur de l'ecran. */
    private float height;
    /** Libelle du modele. */
    private TextView libelle;
    /** Image du modele. */
    private ImageButton modele;
    /** Image des points. */
    private ImageView points;
    /** Points. */
    private String strPoints;

    /** Constructeur.
     * @param pContext */
    public LevelView(final Context pContext) {
        super(pContext);
    }

    /** Constructeur.
     * @param pContext
     * @param pAttrs
     * @param pDefStyle */
    public LevelView(final Context pContext, final AttributeSet pAttrs,
            final int pDefStyle) {
        super(pContext, pAttrs, pDefStyle);
    }

    /** Constructeur.
     * @param pContext
     * @param pAttrs */
    public LevelView(final Context pContext, final AttributeSet pAttrs) {
        super(pContext, pAttrs);
    }

    /** Initialisation. */
    public void init(final float pWidth, final float pHeight) {
        this.width = pWidth;
        this.height = pHeight;

        this.setOrientation(VERTICAL);

        final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1);
        params.gravity = Gravity.CENTER_HORIZONTAL;
        params.setMargins(10, 2, 10, 5);

        this.libelle = new TextView(this.getContext());
        this.libelle.setGravity(Gravity.CENTER_HORIZONTAL);

        this.modele = new ImageButton(this.getContext());

        this.points = new ImageButton(this.getContext());
        this.points.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(final View pView) {
                Toast.makeText(LevelView.this.getContext(),
                        LevelView.this.strPoints, Toast.LENGTH_LONG).show();
            }
        });

        this.addView(this.libelle, params);
        this.addView(this.modele, params);
        this.addView(this.points, params);
    }

    public void setModele(final Dessin dessin, final boolean lock) {
        this.libelle.setText(dessin.getName());
        this.libelle.setTextColor(getResources().getColor(R.color.black_overlay));
        Drawable background = null;
        if (lock) {
            final Bitmap bitmap = BitmapFactory.decodeResource(this
                    .getResources(), R.drawable.lock);
            background = new BitmapDrawable(this.getResources(), Bitmap
                    .createScaledBitmap(bitmap, (int) this.width,
                            (int) this.height, false));
        } else {
            background = new BitmapDrawable(this.getResources(), dessin.draw(
                    this.width, this.height));
            final LevelsActivity parent = (LevelsActivity) this.getContext();
            this.modele.setOnClickListener(parent.clickModele(dessin));
        }
        this.modele.setImageDrawable(background);
    }

    /** @param pNbPoints the nbPoints to set */
    public void setNbPoints(final int nbPoints) {
        this.strPoints = String.valueOf(nbPoints) + "/3000";
        if (nbPoints < 400) {
            this.points.setBackgroundResource(R.drawable.points0);
        } else if (nbPoints < 900) {
            this.points.setBackgroundResource(R.drawable.points1);
        } else if (nbPoints < 1400) {
            this.points.setBackgroundResource(R.drawable.points2);
        } else if (nbPoints < 1900) {
            this.points.setBackgroundResource(R.drawable.points3);
        } else if (nbPoints < 2400) {
            this.points.setBackgroundResource(R.drawable.points4);
        } else {
            this.points.setBackgroundResource(R.drawable.points5);
        }
    }

}
